package com.miraclengine.karma;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import com.miraclengine.framework.Game;
import com.miraclengine.framework.Input.TouchEvent;
import com.miraclengine.framework.gl.Camera2D;
import com.miraclengine.framework.gl.FPSCounter;
import com.miraclengine.framework.gl.SpriteBatcher;
import com.miraclengine.framework.impl.GLScreen;
import com.miraclengine.framework.math.OverlapTester;
import com.miraclengine.framework.math.Rectangle;
import com.miraclengine.framework.math.Vector2;

public class KarmaMenuScreen extends GLScreen {
	
	Camera2D guiCam;
	SpriteBatcher batcher;
	
	Vector2 touchPoint;
	
	float counter = 0;
	
	boolean bBtnStart = false;
	boolean bBtnReset = false;
	
	Rectangle startBounds;
	Rectangle resetBounds;

	public KarmaMenuScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
		guiCam = new Camera2D(glGraphics, 1184, 720);
		batcher = new SpriteBatcher(glGraphics, 100);
		
		touchPoint = new Vector2();
		
		startBounds = new Rectangle(366-394/2, 159-140/2, 394, 140);
		resetBounds = new Rectangle(831-394/2, 159-140/2, 394, 140);
	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		for(TouchEvent event : touchEvents) {
			
			touchPoint.set(event.x, event.y);
			guiCam.touchToWorld(touchPoint);
			
			Log.d("TAG", "x : " + touchPoint.x + ", y : " + touchPoint.y);
			
			if(event.type == TouchEvent.TOUCH_UP) {
				if(OverlapTester.pointInRectangle(startBounds, touchPoint)) {//스타트를 눌렀을 때.
					bBtnStart = false;
				} else if(OverlapTester.pointInRectangle(resetBounds, touchPoint)) {
					bBtnReset = false;
				}
			} else if(event.type == TouchEvent.TOUCH_DOWN) {
				if(OverlapTester.pointInRectangle(startBounds, touchPoint)) {//스타트를 눌렀을 때.
					bBtnStart = true;
				} else if(OverlapTester.pointInRectangle(resetBounds, touchPoint)) {
					bBtnReset = true;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.image_data);
        batcher.drawSprite(592+108, 360, 1400, 720, Assets.kBackgroundRegion);
        batcher.drawSprite(592, 448, 557, 356, Assets.kTitleRegion);
        if(!bBtnStart)
        	batcher.drawSprite(366, 159, 394, 140, Assets.kBtn_start_normalRegion);
        else 
        	batcher.drawSprite(366, 159, 394, 140, Assets.kBtn_start_selectRegion);
        if(!bBtnReset)
        	batcher.drawSprite(831, 159, 394, 140, Assets.kBtn_reset_normalRegion);
        else 
        	batcher.drawSprite(831, 159, 394, 140, Assets.kBtn_reset_selectRegion);
        
        batcher.drawSprite(592, 51, 304, 25, Assets.kCopyrightRegion);
        
        batcher.endBatch();
        
        
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        //fps.logFrame();
	}
	
	FPSCounter fps = new FPSCounter();

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
